package com.newcapec.servlet;

import com.newcapec.service.SysUserService;
import com.newcapec.service.impl.SysUserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description:删除用户
 * @Author:yangshupeng
 * @Date: 2022/3/17 20:57
 * @Version:1.0
 */
@WebServlet("/delUser")
public class DelUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SysUserService sysUserService = new SysUserServiceImpl();
        //获取要删除的单个id
        String id = req.getParameter("id");
        //获取批量删除的id数组
        String checkId = req.getParameter("checkId");
        if (checkId != null){
            String[] str = checkId.split(",");//切割字符串返回数组
            int[] ids = new int[str.length];//定义一个数组接收这个数组，用来强转
            if (str.length > 0){
                for (int i = 0; i < str.length; i++){
                    if (!str[i].equals("null")){
                        ids[i] = Integer.parseInt(str[i]);//强转成int类型的数组
                    }

                }
            }
            for (int id1 : ids){//循环删除
                sysUserService.deleteUser(String.valueOf(id1));
            }
            resp.sendRedirect("AccountManagement.html");
        }else {
            resp.getWriter().write("no");
        }



        if (id != null){
            sysUserService.deleteUser(id);
            resp.sendRedirect("AccountManagement.html");
        }else {
            resp.getWriter().write("no");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
